import tensorflow as tf

X = tf.constant([[1.0, 2.0], [3.0, 4.0]])
y = tf.constant([[1.0], [2.0]])
w = tf.Variable(initial_value=[[1.0], [2.0]])
b = tf.Variable(initial_value=1.0)
with tf.GradientTape() as tape:
    L = tf.reduce_sum(tf.square(tf.matmul(X, w) + b - y))
w_grad, b_grad = tape.gradient(L, [w, b])  # 计算L(w, b)关于w, b的偏导数
print(L, w_grad, b_grad)
